<?php
/**
 * 注：此模板为自动生成模板，需要根据实际情况调整修改
 * 此模板对 无主键/单一主键 表的支持较好，联合主键表在下一版本开发
 */
namespace Data;

use Utils\MyException;

class Tree_productor_hist_DAO {

    public static $TABLENAME = 'Tree_productor_hist';

    public static $_db;

    public static function query($str)
    {
        if(self::$_db === null) {
            self::$_db = \GatewayWorker\Lib\Db::instance('main');
            return self::$_db->query($str);
        } else {
            return self::$_db->query($str);
        }
    }




    public static function getRecord($PRODUCTOR_ID){
        $where = "`PRODUCTOR_ID` = '$PRODUCTOR_ID'";
        $record = self::query('select * from '.static::$TABLENAME .' where '.$where);
        if($record != null){
            return new Tree_productor_hist($record[0]);
        }
        return null;
    }



    public static function getList() {
        $where = "";
        $list = self::query('select * from '.static::$TABLENAME);
        $results = array();

        foreach($list as $record){
            $results[] = new Tree_productor_hist($record);
        }
        return $results;
    }


    public static function getListByProductor_id($productor_id) {
        $where = "`PRODUCTOR_ID`='$productor_id' order by `DATE` DESC";
        $list = self::query('select `PRICE`,`DATE` from '.static::$TABLENAME.' where '.$where);
        return $list;
    }


    public static function getListByVisitCount($needle,$province,$city,$county,$date,$list_start,$every_page_counts) {
        $where = "1=1";
        if ($province) {
            $where .= " AND `PROVINCE`='$province'";
        }
        if ($city) {
            $where .= " AND `CITY`='$city'";
        }
        if ($county) {
            $where .= " AND `COUNTY`='$county'";
        }
        if ($date) {
            $where .= " AND `DATE`='$date'";
        }
        $total_count = self::query('select COUNT(*) as TOTAL_COUNT from '.static::$TABLENAME.' where '.$where);

        $where .= " ORDER BY `VISIT_COUNT` DESC limit $list_start,$every_page_counts";
        $list = self::query('select * from '.static::$TABLENAME.' where '.$where);
        $results = array();
        $results[0] = $total_count[0]['TOTAL_COUNT'];
        foreach($list as $record){
            $results[1][] = new Tree_productor($record);
        }
        return $results;
    }


    public static function insert($record){

        try {

            $PRODUCTOR_ID = $record->getPRODUCTOR_ID();
            $oldrecord = self::getRecord($PRODUCTOR_ID);
            if($oldrecord != null) {
                throw new MyException ("重复插入");
            }
            $sql = 'insert into '.static::$TABLENAME .' values ('.$record->toInsertCSV().')' ;
            self::query($sql);
            return self::$_db->lastInsertId();
        } catch(MyException $e){
            throw $e;
        }
    }


    public static function insertEveryMonth($record){

        try {

            $sql = 'insert into '.static::$TABLENAME .' values ('.$record->toInsertCSV().')' ;
            self::query($sql);
            return self::$_db->lastInsertId();
        } catch(MyException $e){
            throw $e;
        }
    }



    public static function update($record){
        try{
            $PRODUCTOR_ID = $record->getPRODUCTOR_ID();
            $where = "`PRODUCTOR_ID` = '$PRODUCTOR_ID'";
            $oldrecord = self::getRecord($PRODUCTOR_ID);
            if($oldrecord == null){
                throw new MyException("不存在该记录");
            }
            $sql = 'update '.static::$TABLENAME .' set '.$record->toUpdateSet().' where '.$where ;
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }



    public static function delete ($record){
        try{
            $PRODUCTOR_ID = $record->getPRODUCTOR_ID();
            $where = "`PRODUCTOR_ID` = '$PRODUCTOR_ID'";
            $oldrecord = self::getRecord($PRODUCTOR_ID);
            if($oldrecord == null){
                throw new MyException("不存在该记录");
            }
            $sql = 'delete from '.static::$TABLENAME . ' where '.$where ;
            self::query($sql);
        } catch(MyException $e){
            throw $e;
        }
    }
}
